package com.beyond.common.dp.theme

import android.content.Context
import androidx.compose.ui.graphics.Color
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import com.beyond.common.ext.dataStore
import com.beyond.common.ext.put
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object ThemeNamePreference {
    private const val THEME_NAME = "themeName"
    const val CUSTOM_THEME_NAME = "Custom"

    val values: List<ThemeItem> = mutableListOf(
        ThemeItem(
            name = "朱砂",
            keyColor = Color(0XFFff461f)
        ),
        ThemeItem(
            name = "宝蓝",
            keyColor = Color(0xFF4b5cc4)
        ),
        ThemeItem(
            name = "青翠",
            keyColor = Color(0xFF00e079)
        ),
    )

    val default = values[0].name

    private val key = stringPreferencesKey(THEME_NAME)

    fun put(context: Context, scope: CoroutineScope, value: String) {
        scope.launch(Dispatchers.IO) {
            context.dataStore.put(key, value)
        }
    }

    fun fromPreferences(preferences: Preferences) = preferences[key] ?: default

    data class ThemeItem(val name: String, val keyColor: Color)
}
